﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CP.Utility;
using DevExpress.XtraEditors.Repository;
using CP.Controllers.GPM;
using System.Linq;
using DevExpress.XtraBars;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

namespace GPM
{
    public partial class BaseForm : DevExpress.XtraEditors.XtraForm
    {
        public System.ComponentModel.IContainer BarComponents;
        public BaseForm()
        {
            InitializeComponent();
        }

        private void BaseForm_Load(object sender, EventArgs e)
        {
            try
            {
                XtraEditorControl();
                XtraGridControl();
#if !DEBUG
                PrivilegeControl();
#endif
            }
            catch (Exception ex)
            {
                MessageBox.Show("BaseForm Error :"+ex.Message);
            }
        }

        protected virtual void PrivilegeControl()
        {
            if (this.DesignMode)
                return;

            MenuController c = new MenuController();
            IList<string> btns = c.GetButtonsByUserForm(AppSetter.CurrentUser.user_id, this.Name);
            if (btns.Count == 0)
                return;
            ControlExtension.GetControls<Button>(this).ForEach(btn =>
            {
                if ((btns.Where(btnName => btnName == btn.Name)).Count() == 0)
                    btn.Visible = false;
                else
                    btn.Visible = true;
            });

            ControlExtension.GetControls<SimpleButton>(this).ForEach(btn =>
            {
                if ((btns.Where(btnName => btnName == btn.Name)).Count() == 0)
                    btn.Visible = false;
                else
                    btn.Visible = true;
            });

            if (this.BarComponents != null)
            {
                if (this.BarComponents.Components != null)
                {
                    foreach (IComponent component in BarComponents.Components)
                    {
                        BarManager barManager = component as BarManager;
                        if (barManager != null)
                        {
                            foreach (BarButtonItem item in barManager.Items.OfType<BarButtonItem>())
                            {
                                if ((btns.Where(btnName => btnName == item.Name).Count() == 0))
                                {
                                    item.Visibility = BarItemVisibility.Never;
                                }
                                else
                                {
                                    item.Visibility = BarItemVisibility.Always;
                                }
                            }

                        }
                    }
                }
            }

        }

        private void XtraGridControl()
        {
            ControlExtension.GetControls<DevExpress.XtraGrid.GridControl>(this).ForEach(gc =>
            {
                gc.KeyDown += gc_KeyDown;
                DevExpress.XtraGrid.Views.Grid.GridView view = gc.MainView as DevExpress.XtraGrid.Views.Grid.GridView;
                if (null != view)
                {
                    view.IndicatorWidth = 30;
                    view.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(view_CustomDrawRowIndicator);
                }

                foreach (DevExpress.XtraEditors.Repository.RepositoryItem rep in gc.RepositoryItems)
                {
                    DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit lookupedit = rep as DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit;
                    if (lookupedit != null)
                    {
                        //lookupedit.KeyDown += new KeyEventHandler(c_KeyDown);
                        lookupedit.DropDownRows = 20;
                        lookupedit.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoFilter;
                        lookupedit.Buttons.Add(new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Delete));
                        lookupedit.ButtonClick += new DevExpress.XtraEditors.Controls.ButtonPressedEventHandler(c_ButtonClick);
                    }
                }
            });
        }

        void gc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.C)
            {
                GridControl grid = sender as GridControl;
                GridView view = grid.FocusedView as GridView;
                Clipboard.SetText(view.GetFocusedDisplayText());
                e.Handled = true;
            }
        }

        private void XtraEditorControl()
        {
            ControlExtension.GetControls<LookUpEdit>(this).ForEach(c =>
            {
                c.Properties.Buttons.Add(new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Delete));
                c.Properties.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoFilter;
                c.ButtonClick += new DevExpress.XtraEditors.Controls.ButtonPressedEventHandler(c_ButtonClick);
                //c.Properties.DropDownRows = 20;
                //c.KeyDown += new KeyEventHandler(c_KeyDown);
            });
        }

        void view_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    //e.Info.DisplayText = (e.RowHandle + 1).ToString();
                    e.Info.DisplayText = ((sender as DevExpress.XtraGrid.Views.Grid.GridView).GetVisibleIndex(e.RowHandle) + 1).ToString();
                }
            }
        }

        void c_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Delete)
                (sender as LookUpEdit).EditValue = null;
        }

        void c_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Back)
                (sender as LookUpEdit).EditValue = null;
        }

        private void BaseForm_Shown(object sender, EventArgs e)
        {
            
        }
    }
}